export enum ERROR {
  UNKNOWN = -500001,
  DB = -400001,
  SVR,
  PARAM,
  BODY,
  QUERY,
  PASSWORD,
  USERNAME,
}

class RestfulError {
  code: number;
  message: string;
  constructor() {
    this.code = ERROR.UNKNOWN;
    this.message = "unknown error";
  }
}

class RestfulResult {
  result: any | undefined = undefined;
  error: RestfulError | undefined = undefined;
}

export function createResult(result: any): RestfulResult {
  let resultObj = new RestfulResult();
  resultObj.result = result;
  return resultObj;
}

export function createError(code: number, msg: string): RestfulResult {
  let resultObj = new RestfulResult();
  resultObj.error = new RestfulError();
  resultObj.error.code = code;
  resultObj.error.message = msg;
  return resultObj;
}

export function responseResult(ctx: any, result: any) {
  ctx.set("Content-Type", "application/json");
  ctx.body = JSON.stringify(createResult(result));
}


export function responseOK(ctx: any) {
    ctx.set("Content-Type", "application/json");
    ctx.body = JSON.stringify(createResult("OK"));
  }
export function responseError(ctx: any, code: number, msg: string) {
  ctx.set("Content-Type", "application/json");
  if(code === 0){
    responseOK(ctx);
    return;
  }
  ctx.body = JSON.stringify(createError(code, msg));
}
